{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "LightGBM模型调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先 import 必要的模块\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "import lightgbm as lgbm\n",
    "from lightgbm.sklearn import LGBMClassifier\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import train_test_split, cross_val_score\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取数据\n",
    "dpath = './data/'\n",
    "train=pd.read_csv(dpath +'FE_train_data_to_category.csv')\n",
    "test=pd.read_csv(dpath +'FE_test_data_to_category.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#object数据类型转换为category类别型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "for col in train.columns:\n",
    "    if train[col].dtype == object:\n",
    "        train[col] = train[col].astype('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "for col in test.columns:\n",
    "    if test[col].dtype == object:\n",
    "        test[col] = test[col].astype('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(7377418, 34)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "#训练模型输入输出参数设置\n",
    "X_train = train.drop(['target'], axis=1)\n",
    "y_train = train['target'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "#测试输入参数设置\n",
    "X_test = test.drop(['id'], axis=1)\n",
    "ids = test['id'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "d_train_final = lgbm.Dataset(X_train, y_train)\n",
    "watchlist_final = lgbm.Dataset(X_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "lightgbm模型训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5]\tvalid_0's auc: 0.733885\n",
      "[10]\tvalid_0's auc: 0.746907\n",
      "[15]\tvalid_0's auc: 0.751987\n",
      "[20]\tvalid_0's auc: 0.757075\n",
      "[25]\tvalid_0's auc: 0.760632\n",
      "[30]\tvalid_0's auc: 0.763948\n",
      "[35]\tvalid_0's auc: 0.766741\n",
      "[40]\tvalid_0's auc: 0.769688\n",
      "[45]\tvalid_0's auc: 0.772217\n",
      "[50]\tvalid_0's auc: 0.774339\n",
      "[55]\tvalid_0's auc: 0.776353\n",
      "[60]\tvalid_0's auc: 0.778272\n",
      "[65]\tvalid_0's auc: 0.780019\n",
      "[70]\tvalid_0's auc: 0.782505\n",
      "[75]\tvalid_0's auc: 0.784213\n",
      "[80]\tvalid_0's auc: 0.785596\n",
      "[85]\tvalid_0's auc: 0.786907\n",
      "[90]\tvalid_0's auc: 0.788159\n",
      "[95]\tvalid_0's auc: 0.789422\n",
      "[100]\tvalid_0's auc: 0.79057\n",
      "[105]\tvalid_0's auc: 0.792374\n",
      "[110]\tvalid_0's auc: 0.794052\n",
      "[115]\tvalid_0's auc: 0.795699\n",
      "[120]\tvalid_0's auc: 0.796927\n",
      "[125]\tvalid_0's auc: 0.797842\n",
      "[130]\tvalid_0's auc: 0.799112\n",
      "[135]\tvalid_0's auc: 0.799967\n",
      "[140]\tvalid_0's auc: 0.800829\n",
      "[145]\tvalid_0's auc: 0.801908\n",
      "[150]\tvalid_0's auc: 0.802607\n",
      "[155]\tvalid_0's auc: 0.803178\n",
      "[160]\tvalid_0's auc: 0.803888\n",
      "[165]\tvalid_0's auc: 0.804985\n",
      "[170]\tvalid_0's auc: 0.805869\n",
      "[175]\tvalid_0's auc: 0.80645\n",
      "[180]\tvalid_0's auc: 0.807317\n",
      "[185]\tvalid_0's auc: 0.808069\n",
      "[190]\tvalid_0's auc: 0.808567\n",
      "[195]\tvalid_0's auc: 0.809279\n",
      "[200]\tvalid_0's auc: 0.80982\n",
      "Wall time: 24min 27s\n"
     ]
    }
   ],
   "source": [
    "params = {\n",
    "        'objective': 'binary',\n",
    "        'metric': 'binary_logloss',\n",
    "        'boosting': 'gbdt',\n",
    "        'learning_rate': 0.3 ,\n",
    "        'verbose': 0,\n",
    "        'num_leaves': 108,\n",
    "        'bagging_fraction': 0.95,\n",
    "        'bagging_freq': 1,\n",
    "        'bagging_seed': 1,\n",
    "        'feature_fraction': 0.9,\n",
    "        'feature_fraction_seed': 1,\n",
    "        'max_bin': 256,\n",
    "        'max_depth': 10,\n",
    "        'num_rounds': 200,\n",
    "        'metric' : 'auc'\n",
    "    }\n",
    "\n",
    "%time model_f1 = lgbm.train(params, train_set=d_train_final,  valid_sets=watchlist_final, verbose_eval=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5]\tvalid_0's auc: 0.733885\n",
      "[10]\tvalid_0's auc: 0.745234\n",
      "[15]\tvalid_0's auc: 0.752127\n",
      "[20]\tvalid_0's auc: 0.756593\n",
      "[25]\tvalid_0's auc: 0.760164\n",
      "[30]\tvalid_0's auc: 0.762891\n",
      "[35]\tvalid_0's auc: 0.764778\n",
      "[40]\tvalid_0's auc: 0.767532\n",
      "[45]\tvalid_0's auc: 0.768427\n",
      "[50]\tvalid_0's auc: 0.769883\n",
      "[55]\tvalid_0's auc: 0.772148\n",
      "[60]\tvalid_0's auc: 0.773821\n",
      "[65]\tvalid_0's auc: 0.776122\n",
      "[70]\tvalid_0's auc: 0.777565\n",
      "[75]\tvalid_0's auc: 0.778895\n",
      "[80]\tvalid_0's auc: 0.779738\n",
      "[85]\tvalid_0's auc: 0.7796\n",
      "[90]\tvalid_0's auc: 0.780063\n",
      "[95]\tvalid_0's auc: 0.781092\n",
      "[100]\tvalid_0's auc: 0.781951\n",
      "[105]\tvalid_0's auc: 0.782966\n",
      "[110]\tvalid_0's auc: 0.782553\n",
      "[115]\tvalid_0's auc: 0.784117\n",
      "[120]\tvalid_0's auc: 0.784713\n",
      "[125]\tvalid_0's auc: 0.785345\n",
      "[130]\tvalid_0's auc: 0.786256\n",
      "[135]\tvalid_0's auc: 0.787002\n",
      "[140]\tvalid_0's auc: 0.787269\n",
      "[145]\tvalid_0's auc: 0.787818\n",
      "[150]\tvalid_0's auc: 0.788303\n",
      "[155]\tvalid_0's auc: 0.789149\n",
      "[160]\tvalid_0's auc: 0.788525\n",
      "[165]\tvalid_0's auc: 0.789682\n",
      "[170]\tvalid_0's auc: 0.790859\n",
      "[175]\tvalid_0's auc: 0.791651\n",
      "[180]\tvalid_0's auc: 0.791535\n",
      "[185]\tvalid_0's auc: 0.792393\n",
      "[190]\tvalid_0's auc: 0.793174\n",
      "[195]\tvalid_0's auc: 0.794116\n",
      "[200]\tvalid_0's auc: 0.794323\n",
      "Wall time: 20min 16s\n"
     ]
    }
   ],
   "source": [
    "params = {\n",
    "        'objective': 'binary',\n",
    "        'metric': 'binary_logloss',\n",
    "        'boosting': 'dart',\n",
    "        'learning_rate': 0.3 ,\n",
    "        'verbose': 0,\n",
    "        'num_leaves': 108,\n",
    "        'bagging_fraction': 0.95,\n",
    "        'bagging_freq': 1,\n",
    "        'bagging_seed': 1,\n",
    "        'feature_fraction': 0.9,\n",
    "        'feature_fraction_seed': 1,\n",
    "        'max_bin': 256,\n",
    "        'max_depth': 10,\n",
    "        'num_rounds': 200,\n",
    "        'metric' : 'auc'\n",
    "    }\n",
    "\n",
    "%time model_f2 = lgbm.train(params, train_set=d_train_final,  valid_sets=watchlist_final, verbose_eval=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Making predictions\n",
      "p_test_1: [0.4730888  0.35172231 0.16619987 ... 0.41710443 0.36297144 0.35152499]\n",
      "p_test_2: [0.47464002 0.52129157 0.17564058 ... 0.37594592 0.39923073 0.39328411]\n",
      "p_test_avg: [0.47386441 0.43650694 0.17092022 ... 0.39652517 0.38110109 0.37240455]\n",
      "Done making predictions\n"
     ]
    }
   ],
   "source": [
    "print('Making predictions')\n",
    "p_test_1 = model_f1.predict(X_test)\n",
    "print('p_test_1:',p_test_1)\n",
    "p_test_2 = model_f2.predict(X_test)\n",
    "print('p_test_2:',p_test_2)\n",
    "p_test_avg = np.mean([p_test_1, p_test_2], axis = 0)\n",
    "print('p_test_avg:',p_test_avg)\n",
    "print('Done making predictions')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saving predictions Model model of gbdt\n",
      "Done!\n"
     ]
    }
   ],
   "source": [
    "print ('Saving predictions Model model of gbdt')\n",
    "\n",
    "subm = pd.DataFrame()\n",
    "subm['id'] = ids\n",
    "subm['target'] = p_test_avg\n",
    "subm.to_csv(dpath + 'submission_lgbm_avg.csv.gz', compression = 'gzip', index=False, float_format = '%.5f')\n",
    "\n",
    "print('Done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
